using System;
using System.CodeDom;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace CodeDom
{
  internal abstract class TemplateFieldGenerator
  {
    private readonly TemplateField _templateField;

    protected TemplateFieldGenerator(TemplateField field)
    {
      _templateField = field;
    }

    public TemplateField TemplateField
    {
      get { return _templateField; }
    }

    public abstract void AddValueConversionStatements(CodeExpression itemReference, CodeMemberMethod method);

    protected CodeExpression CreateFieldValueExpression(CodeExpression itemReference)
    {
      CodeExpression fieldID = new CodeObjectCreateExpression("ID", new CodePrimitiveExpression(TemplateField.ID.ToString()));
      CodeIndexerExpression indexer = new CodeIndexerExpression(itemReference, fieldID);
      return indexer;
    }

    public abstract CodeTypeReference GetFieldType();

    protected CodeVariableDeclarationStatement CreateFieldValueVariableStatement(CodeExpression itemReference)
    {
      CodeExpression getValue = CreateFieldValueExpression(itemReference);
      var valueVariable = new CodeVariableDeclarationStatement("String", "fieldValue", getValue);
      return valueVariable;
    }

    protected CodeMethodInvokeExpression CreateNullCheck(CodeExpression expression)
    {
      return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("String"), "IsNullOrEmpty", expression);
    }
  }
}